/* eslint-disable max-lines */
import TauSimpleSelect from '@/components/TauSimpleSelect';
import { ellipsisRender } from '@/components/TauTable';
import * as BASE from '@/stores/definitions-base';
import { Input } from '@alifd/next';
import { t } from '@lingui/macro';
import React from 'react';

const {
  DATETIME_TEXT,
  DATE_TEXT,
  TIME_TEXT,
  EMPTY_TEXT,
  ENUMS: ENUMSBase,
  FieldsGrid: FieldGridBase,
  TitleGrid,
  column: columnBase,
  toSelectDataSource,
} = BASE;
export { DATETIME_TEXT, DATE_TEXT, EMPTY_TEXT, TIME_TEXT, TitleGrid, toSelectDataSource };

/**
 * @param {*} path        属性path
 * @param {*} props       column属性
 * @returns column定义
 */
export function column(...args) {
  return columnBase(model, ...args);
}

/**
 * 显示一组表单字段
 * ```js
 * {
 *   fields: string[],    // 字段名称，可以使用.来指定嵌套对象属性，例如order.busOrder.shift
 *   columns: number,     // 列数，默认为4,最大为12,最小为1
 *   model: object,       // 字段定义数据模型的根对象，默认不指定，仅在使用子对象作为根模型时设置
 *   overrideModel: object, // 重载字段定义数据模型
 *   mode: string,        // 表单模式，默认为edit，可选view
 *   itemLayout: object,  // FormItem布局配置，默认为{}
 * }
 * ```
 *
 * @param {object} param0
 * @returns
 */
export function FieldsGrid(props) {
  return <FieldGridBase model={model} {...props} />;
}

//----------------------------------------------------------------------
export const ENUMS = {
  ...ENUMSBase,
  // <br/> 0-取消<br/> 1-已接单<br/> 2-已上客<br/> 3-已下客
  status: {
    0: t`取消`,
    1: t`发布中`,
    2: t`已接单`,
    3: t`已上客`,
    4: t`已下客`,
    5: t`已完成`,
  },
  driverStatus: {
    0: t`取消`,
    1: t`已接单`,
    2: t`已上客`,
    3: t`已下客`,
  },
};

/**
 * 模型定义：
 * 1. 普通文本仅需要定义label
 * 2. 需要通过枚举转换的值，定义text; 当使用renderer返回组件时，text属性无效，需要通过组件的renderPreview来指定预览时的绘制方式
 * 3. 通用的ui属性需要定制时，定义ui; 特定模式(view, edit, add)下的ui，添加后缀定义为ui_<mode>，例如：ui_view, ui_edit, ui_add, ui_table
 * 4. ui可配置属性包括:
 *    renderer: 内容渲染函数, Form表单中的原型为：(value: any, formData: any, fieldDefinition: any) => ReactNode; Table Column中的原型为: (value: any, columnIndex: number, rowData: any) => ReactNode;
 *    itemProps: 表单Form.Item的属性，可以是对象，或函数({ isPreview: boolean, mode: string, value: any, formData: any, ...otherDefinitions}) => void;
 *    span: Grid Cell占用的列数, 默认为1
 *
 * 【注】获取接口的model定义后，vscode中通过正则替换：^(\s+)"label": "([^']+)"  ===> $1label: t`$2`
 */
export const model = {
  taskId: {
    label: t`任务ID`,
  },
  status: {
    label: t`任务状态`,
    text: (v) => <>{ENUMS.status[v]}</>,
    ui: {
      renderer: (v) => (
        <TauSimpleSelect value={v} dataSource={toSelectDataSource(ENUMS.status)} />
      ),
    },
  },
  groupId: {
    label: t`群id`,
  },
  groupName: {
    label: t`群名称`,
  },
  publisherId: {
    label: t`发布人id`,
  },
  publisherName: {
    label: t`发布人姓名`,
  },
  publisherGroup: {
    label: t`发布人姓名/号码`,
    text: (v, r) => <>{`${r?.publisherName || '无'}/${r?.publisherPhone || '无'}`}</>,
  },
  publisherPhone: {
    label: t`发布人手机`,
  },
  orderTakerId: {
    label: t`接单人id`,
  },
  orderTakeGroup: {
    label: t`接单人姓名/号码`,
    text: (v, r) => {
      const { driverName, driverPhone } = rootStore.taskStore._table_driver(r?.driverInfos) || {};
      return <>{`${driverName || '无'}/${driverPhone || '无'}`}</>;
    },
  },
  takeOrderTime: {
    label: t`接单时间`,
    text: (v, r) => {
      const { takeOrderTime } = rootStore.taskStore._table_driver(r?.driverInfos) || {};
      return <>{takeOrderTime ? DATETIME_TEXT(takeOrderTime) : t`无`}</>;
    },
  },
  orderTakerName: {
    label: t`接单人姓名`,
  },
  orderTakerPhone: {
    label: t`接单人手机`,
  },
  issueOrderPoint: {
    label: t`发单积分`,
  },
  takeOrderPoint: {
    label: t`接单积分`,
  },
  pointGroup: {
    label: t`发/接单积分`,
    text: (v, r) => {
      const { issueOrderPoint, takeOrderPoint } = r;
      return <>{`${issueOrderPoint} / ${takeOrderPoint}`}</>;
    },
  },
  predictBeginDate: {
    label: t`预估开始时间`,
    text: DATETIME_TEXT,
  },
  price: {
    label: t`价格`,
  },
  crDate: {
    label: t`发单时间`,
    text: DATETIME_TEXT,
  },
  content: {
    label: t`发布内容`,
    text: (v) => <>{ellipsisRender(v)}</>,
    ui: {
      renderer: (v) => <Input.TextArea value={v} />,
      span: 3,
    },
  },
  driverInfos: {
    label: t`接单司机信息`,
    driverId: {
      label: t`接单司机id`,
    },
    driverName: {
      label: t`接单司机名字`,
    },
    driverPhone: {
      label: t`接单司机号码`,
    },
    driverGroup: {
      label: t`接单司机名字/号码`,
    },
    takeOrderTime: {
      label: t`接单时间`,
      text: DATETIME_TEXT,
    },
    status: {
      label: t`司机状态`,
    },
  },
  remark: {
    label: t`备注`,
    ui: {
      span: 4,
    },
  },
};
